package com.platform.common.util.encrypt;


/**
 * 
 * Description: 述本文件的内容、功能、内部各部分之间的关系及本文件与其它文件关系等
 */
public class DESEncryptUtil {

	/**
	 * 加密
	 * 
	 * @param key
	 * @param sPwd
	 * @return
	 */
	public static String enCode(String key, String sPwd) {
		String results = "";
		try {
			results = new Encrypt().hsEncrypt(key.toCharArray(), sPwd.toCharArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}

	/**
	 * 解密
	 * 
	 * @param key
	 * @param sEnCoded
	 * @return
	 */
	public static String deCode(String key, String sEnCoded) {
		String results = "";
		try {
			results = new Encrypt().hsDecrypt(key.toCharArray(), sEnCoded.toCharArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}
}

class Encrypt extends DES {

	/**
	 * 
	 * 加密
	 * 
	 * @param key
	 *            密钥key,长度小于等于15
	 * @param paintxt
	 *            明文txt,长度小于等于16
	 * @return
	 */
	public String hsEncrypt(char[] key, char[] paintxt) throws Exception {
		char[] pain = new char[33];
		try {
			this.hsEncrypt(key, paintxt, pain);
		} catch (Exception ex) {
			throw ex;
		}
		StringBuffer results = new StringBuffer();
		for (char et : pain) {
			if (et == '\0') {
				break;
			}
			results.append(et);
		}
		return results.toString();
	}

	/**
	 * 解密
	 * 
	 * @param key
	 *            密钥key,长度小于等于15
	 * @param ciphertxt
	 *            密文txt,长度等于32
	 * @return
	 */
	public String hsDecrypt(char[] key, char[] ciphertxt) throws Exception {
		char[] pass = new char[32];
		try {
			this.hsDecrypt(key, ciphertxt, pass);
		} catch (Exception ex) {
			throw ex;
		}
		StringBuffer results = new StringBuffer();
		for (char et : pass) {
			if (et == '\0') {
				break;
			}
			results.append(et);
		}
		return results.toString();
	}

	/**
	 * 使用2des加密,'16长度'结果输出为'32长度'hex进制
	 * 
	 * @param key
	 * @param paintxt
	 *            输入明文
	 * @param ciphertxt
	 *            输出结果
	 */
	void hsEncrypt(char[] key, char[] paintxt, char[] ciphertxt) throws Exception {
		char key2[] = new char[16], tmp1[] = new char[16], tmp2[] = new char[16];
		char tmpKey[] = new char[16];
		int i;

		if (paintxt.length > 16) {
			throw new Exception("输入明文太长,超过16个字符");
		}
		if (key.length > 15) {
			throw new Exception("输入密钥太长,超过15个字符");
		}

		for (int m = 0; m < key.length; m++) {
			tmpKey[m] = key[m];
		}
		make2key(tmpKey, key2);
		des2key(key2, true);
		for (int m = 0; m < paintxt.length; m++) {
			tmp1[m] = paintxt[m];
		}
		//
		D2des(tmp1, tmp2);
		//
		for (i = 0; i < 16; i++) {
			ciphertxt[i * 2] = (char) (tmp2[i] / 16 + ((tmp2[i] / 16 >= 10) ? ('A' - 10) : '0'));
			ciphertxt[i * 2 + 1] = (char) (tmp2[i] % 16 + ((tmp2[i] % 16 >= 10) ? ('A' - 10) : '0'));
		}
		ciphertxt[32] = '\0';
	}

	/**
	 * 使用2des解密,'32长度'hex进制,先恢复为'16长度'密文.
	 * 
	 * @param key
	 * @param ciphertxt
	 * @param paintxt
	 */
	void hsDecrypt(char[] key, char[] ciphertxt, char[] paintxt) throws Exception {
		char key2[] = new char[16], tmp1[] = new char[16], tmp2[] = new char[16];
		char tmpKey[] = new char[16];
		int i;

		// if (ciphertxt.length > 32) {
		// ciphertxt[32] = '\0';
		// }

		if (ciphertxt.length > 32) {
			throw new Exception("输入密文太长,超过32个字符");
		}

		if (key.length > 15) {
			throw new Exception("输入密钥太长,超过15个字符");
		}

		for (int m = 0; m < key.length; m++) {
			tmpKey[m] = key[m];
		}
		//
		make2key(tmpKey, key2);
		//
		des2key(key2, false);
		// 恢复字符串
		for (i = 0; i < 32; i += 2) {
			tmp1[i / 2] = (char) ((ciphertxt[i] - ((ciphertxt[i] >= 'A') ? ('A' - 10) : '0')) * 16 + (ciphertxt[i + 1] - ((ciphertxt[i + 1] >= 'A') ? ('A' - 10)
					: '0')));
		}
		//
		D2des(tmp1, tmp2);
		//
		for (int m = 0; m < 16; m++) {
			paintxt[m] = tmp2[m];
		}
		// s2[32] = '\0';
	}
}